package com.xdwanj.criminalintent.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.xdwanj.criminalintent.R;
import com.xdwanj.criminalintent.fragment.CrimeListFragment;
import com.xdwanj.criminalintent.pojo.Crime;

import java.util.List;

//适配器
public class CrimeAdapter extends RecyclerView.Adapter<CrimeAdapter.ViewHolder> {
    private final String TAG = "CrimeAdapter";

    //数据
    private final List<Crime> crimes;

    private final CrimeListFragment context;

    //这个就是MainActivity,由于传入的是个接口,只能调用这个接口中的共有方法
    private final CrimeListFragment.Callbacks callbacks;

    //构造方法传入视图所需的数据
    public CrimeAdapter(CrimeListFragment context, List<Crime> crimes, CrimeListFragment.Callbacks callbacks) { //传入数据集合
        this.crimes = crimes;
        this.context = context;
        this.callbacks = callbacks;
    }

    @NonNull
    @Override
    //绑定布局文件并创建ViewHolder(视图持有者)
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //在在布局中填充 list_item_crime 布局
        View view = context.getLayoutInflater().inflate(R.layout.list_item_crime, parent, false);

        //定义 ViewHolder
        ViewHolder crimeHolder = null;

//            switch (viewType) { //此处判断
//                case 1:
//                    //
//                    break;
//                default:
//                    //
//                    break;
//            }

        crimeHolder = new ViewHolder(view); //视图适配器与填充所用的布局绑定(传入)
        //这里可以通过判断viewType来构建Holder
        return crimeHolder;
    }

    @Override
    //将视图数据集合切成小块,分别给指定的 ViewHolder
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) { //绑定数据
        //将具体控件内容绑定数据封装如Holder内部
        Log.i(TAG, "onBindViewHolder: crime->" + crimes.get(position));
        holder.bing(crimes.get(position));
    }

    @Override
    //获取数据集合大小
    public int getItemCount() { //获取数据个数大小
        return crimes.size();
    }

    @Override
    public int getItemViewType(int position) {
        //重写这个方法返回pojo中的type:int数据来返回,知道用什么样的布局文件
        return super.getItemViewType(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private static final String TAG = "ViewHolder";

        private final TextView titleTextView;
        private final TextView dateTextView;
        private final ImageView solvedImageView;

        // 这个crime来自Adapter传入绑定方法
        private Crime crime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            //在构造方法中获取ItemView中的控件,方便后面将数据展示
            titleTextView = (TextView) itemView.findViewById(R.id.crime_title);
            dateTextView = (TextView) itemView.findViewById(R.id.crime_date);
            solvedImageView = (ImageView) itemView.findViewById(R.id.crime_solved);

            //这个事件绑定可以放在bing方法里也可以就在此处
            itemView.setOnClickListener(v -> {
                Log.d(TAG, "ViewHolder: " + this.crime.getId() + " | " + this.crime.toString());
                callbacks.onCrimeSelected(this.crime.getId()); //这个方法来自MainActivity
            });
        }

        /**
         * 自定义方法,负责绑定数据,供Adapter调用
         *
         * @param crime 数据块
         */
        public void bing(Crime crime) {
            this.crime = crime;
            //展示数据
            titleTextView.setText(crime.getTitle());
            dateTextView.setText(crime.getDate().toString());
            if (crime.isSoled()) {
                solvedImageView.setVisibility(View.VISIBLE);
            } else {
                solvedImageView.setVisibility(View.GONE);
            }
        }

    }
}